export default {
    data() {
        return {
            rehearsalID: ''
        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        }
    },
    watch: {
        basin: {
            handler: function (name) {
                this.$nextTick(() => {
                    this.creatDMX(name)
                })
            },
            immediate: true
        },
        pageState: {
            immediate: true,
            deep: true,
            handler: function (val, oldVal) {
                if (this.$route.path === '/Rehearsal') {
                    this.rehearsalID = val.planId;
                }
                if (this.$route.path === '/reserveplan/visualization') {
                    this.rehearsalID = val.planId;
                }
            }
        }
    },
    created() {
        this._dataSourceDMX = null
        // this.$eventBus.$on('DMXRehearsalID', id => {
        //     this.rehearsalID = id
        // })
    },
    methods: {
        creatDMX(name) {
            this.clearDMX()
            if (this.layerList2[1].children.DMX.show) {
                this._dataSourceDMX = new Cesium.CustomDataSource('PolylineDataSource')
                var url = `${mapConfig.resUrlPre}/geojson/${this.basin}/DMX.json`
                this.$axios.get(url).then(res => {
                    if (!res.data || !res.data || !res.data.features) {
                        console.log('没有找到断面数据', url)
                        return
                    }
                    res.data.features.forEach(feature => {
                        // console.log(feature)
                        var lonlats = []
                        if (feature.geometry.type === 'LineString') {
                            feature.geometry.coordinates.forEach(coordinate => {
                                lonlats.push(...coordinate)
                            })
                        } else if (feature.geometry.type === 'MultiLineString') {
                            feature.geometry.coordinates.forEach(coordinates => {
                                coordinates.forEach(coordinate => {
                                    lonlats.push(...coordinate)
                                })
                            })
                        } else {
                            console.log('An error occurred')
                        }

                        var polylineEntity = {
                            eventName: '断面',
                            prop: feature.properties,
                            polyline: {
                                positions: Cesium.Cartesian3.fromDegreesArray(lonlats),
                                width: 5,
                                material: Cesium.Color.fromCssColorString('rgb(157, 66, 71)'),
                                clampToGround: true,
                                classificationType: Cesium.ClassificationType.BOTH // BOTH  or TERRAIN
                            }
                        }
                        this._dataSourceDMX.entities.add(polylineEntity)
                    })
                    this.$viewer.dataSources.add(this._dataSourceDMX)
                    this.initEvevtDMX()
                })
            } else {
                this.clearDMX()
            }
        },
        initEvevtDMX() {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName &&
                    pickedObject.id.eventName === '断面'
                ) {
                    console.log(pickedObject.id);
                    console.log(this.rehearsalID);
                    // if (this.rehearsalID !== '') {
                        const dlgParams = this.$store.getters.dlgParams

                        dlgParams.name = 'SectionWaterLevel'
                        if (this.rehearsalID !== '') {
                            dlgParams.params = {

                                rehearsalID: this.rehearsalID, // 方案id

                                id: this.basin, // 流域编码

                                sectionName: pickedObject.id.prop.NAME || pickedObject.id.prop.Name // 断面名称

                            }
                        }else{
                            dlgParams.params = {
                                rehearsalID: '', // 方案id
                                id: this.basin, // 流域编码
                                sectionName: pickedObject.id.prop.NAME || pickedObject.id.prop.Name, // 断面名称
                                properties:pickedObject.id.prop,//被选中的数据
                            }
                        }
                        
                        this.$store.dispatch('dlgParams', dlgParams)
                    // }
                }
            }
            this.$eventBus.$on('UE_RehearsalDMX', res => {
                eventFn(res)
            })
            var eventCesiumOption = this.$store.getters.eventCesiumOption
            eventCesiumOption.leftPickFns.set('断面', eventFn)
        },
        clearDMX() {
            if (this._dataSourceDMX) this.$viewer.dataSources.remove(this._dataSourceDMX)
        }
    }
}
